﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;
using TestOnlineProject.Models.DataAccess;

namespace TestOnlineProject.Models.BusinessLogic
{
    public class CustomTestBLO
    {
        QuestionTypeDAO questionTypeDAO = new QuestionTypeDAO();
        SectionDAO sectionDAO = new SectionDAO();
        TestTypeBLO testTypeBLO = new TestTypeBLO();
        TestDAO testDAO = new TestDAO();

        public CustomTestModel GenerateCustomTestModel(int subjectId, int testId)
        {
            CustomTestModel customTestModel = new CustomTestModel();
            if (subjectId != 0 && testId == 0)
            {
                customTestModel.SubjectId = subjectId;
                customTestModel.UserId = int.Parse(HttpContext.Current.User.Identity.Name);
                return customTestModel;
            }
            if (subjectId == 0 && testId != 0)
            {
                var test = testDAO.SelectByTestId(testId);
                customTestModel.SubjectId = test.SubjectId;
                customTestModel.TestTypeId = test.TestTypeId;
                customTestModel.TestId = test.TestId;
                customTestModel.UserId = test.UserId;
                customTestModel.SectionId = 0;
                customTestModel.Title = test.Title;
                customTestModel.Duration = test.Duration;
                customTestModel.Expires = test.Expires.Year + "-" + test.Expires.Month + "-" + test.Expires.Day
                    + " " + test.Expires.Hour + ":" + test.Expires.Minute;
                customTestModel.NumberOfQuestions = test.TestQuestions.Where(q => q.Question.IsActivated).ToList().Count;
                customTestModel.LastEditedTime = test.LastEditedTime;
                return customTestModel;
            }
            return customTestModel;
        }
    }
}